package com.example.dangerouscargodriver.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.baiju.netmanager.Http.HttpRequest;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.netmanager.Http.INetResponse;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.widget.LoadingDialog;

import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.HashMap;

public abstract class HttpRequestActivity extends AppCompatActivity {

    private static final String TAG = "HttpRequestActivity";
    private static final int MSG_GET_RESPONSE = 0;
    private static final int MSG_GET_ERROR_RESPONSE = 1;

    private HttpRequest mHttp = null;
    private HttpResponseListener mHttpListener = null;
    private java.lang.Class mObject = null;
    private HttpRequestHandler mHttpHandler = null;

    private LoadingDialog mLoadingDialog = null;

    protected void initHttpComponent(java.lang.Class object){
        mObject = object;
        mHttpListener = new HttpResponseListener();
        mHttpHandler = new HttpRequestHandler(this);
        mHttp = HttpRequest.Instance(getBaseContext());
        mHttp.attachResponseListener(mHttpListener, object);
        mHttp.startUp();

        mLoadingDialog = new LoadingDialog(this);
    }

    protected void sendHttpRequest(IHttpRequest.RequestMethod method,
                                   String url,
                                   HashMap<String, String> header,
                                   HashMap<String, String> param,
                                   int actionId){
        HashMap<String, String> wrapper = new HashMap<>();
        wrapper.put("app-type", "Android");
        if(header != null){
            for (String key : header.keySet()) {
                wrapper.put(key, header.get(key));
            }
        }
        mHttp.sendRequest(method, url, wrapper, param, actionId, mObject);
    }

    protected void sendHttpMultipartRequest(String url,
                                   HashMap<String, String> header,
                                   HashMap<String, String> param,
                                   String file,
                                   int actionId){
        HashMap<String, String> wrapper = new HashMap<>();
        wrapper.put("app-type", "Android");
        if(header != null){
            for (String key : header.keySet()) {
                wrapper.put(key, header.get(key));
            }
        }

        mHttp.sendMultipartRequest(url, param, wrapper, file, actionId, mObject);
    }

    protected void sendHttpFormRequest(String url,
                                       HashMap<String, String> header,
                                       HashMap<String, String> param,
                                       int actionId){
        HashMap<String, String> wrapper = new HashMap<>();
        wrapper.put("app-type", "Android");
        if(header != null){
            for (String key : header.keySet()) {
                wrapper.put(key, header.get(key));
            }
        }

        mHttp.sendMultipartRequest(url, param, wrapper, null, actionId, mObject);
    }

    protected void sendHttpJsonRequest(String url,
                                       HashMap<String, String> header,
                                       JSONObject object,
                                       int actionId){
        HashMap<String, String> wrapper = new HashMap<>();
        wrapper.put("app-type", "Android");
        if(header != null){
            for (String key : header.keySet()) {
                wrapper.put(key, header.get(key));
            }
        }

        mHttp.sendJsonRequest(IHttpRequest.RequestMethod.POST_METHOD,
                url,
                wrapper,
                object,
                actionId,
                mObject);
    }

    protected void sendHttpJsonRequest(IHttpRequest.RequestMethod method,
                                       String url,
                                       HashMap<String, String> header,
                                       JSONObject object,
                                       int actionId){
        HashMap<String, String> wrapper = new HashMap<>();
        wrapper.put("app-type", "Android");
        if(header != null){
            for (String key : header.keySet()) {
                wrapper.put(key, header.get(key));
            }
        }

        mHttp.sendJsonRequest(method,
                url,
                wrapper,
                object,
                actionId,
                mObject);
    }
    
    protected void showLoadingDialog(){
        mLoadingDialog.show();
    }

    protected void dismissLoadingDialog(){
        mLoadingDialog.dismiss();
    }

    protected void noticeOffline(){
        ToastUtils.showLongToast(getBaseContext(), "与服务器通信异常");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        mHttp.unAttachResponseListener(mHttpListener, mObject);
    }

    private class HttpResponseListener implements INetResponse {

        private static final String TAG = "HttpResponseListener";

        @Override
        public void onResponse(int actionId, String response) {
            Message msg = new Message();
            msg.what = MSG_GET_RESPONSE;
            Bundle bundle = new Bundle();
            bundle.putInt("action", actionId);
            bundle.putString("response", response);
            msg.setData(bundle);
            mHttpHandler.sendMessage(msg);
        }

        @Override
        public void onErrorResponse(int actionId, String response) {
            Message msg = new Message();
            msg.what = MSG_GET_ERROR_RESPONSE;
            Bundle bundle = new Bundle();
            bundle.putInt("action", actionId);
            bundle.putString("response", response);
            msg.setData(bundle);
            mHttpHandler.sendMessage(msg);
        }
    }

    protected abstract void onHttpResponse(int actionId, String response);

    protected abstract void onHttpErrorResponse(int actionId, String response);

    private static class HttpRequestHandler extends Handler {
        private String TAG = "HttpRequestHandler";
        private final WeakReference<HttpRequestActivity> mActivity;

        HttpRequestHandler(HttpRequestActivity activity) {
            mActivity = new WeakReference<HttpRequestActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            HttpRequestActivity activity = mActivity.get();
            Bundle bundle = msg.getData();
            switch (msg.what){
                case MSG_GET_RESPONSE:
                    activity.onHttpResponse(bundle.getInt("action"),
                            bundle.getString("response"));
                    break;
                case MSG_GET_ERROR_RESPONSE:
                    activity.onHttpErrorResponse(bundle.getInt("action"),
                            bundle.getString("response"));
                default:
                    break;
            }
        }
    }
}
